﻿using System.IO;
using System.Text;
using System.Web.UI;
using Moq;
using NUnit.Framework;
using OpenWaves.ImageTransformations;
using OpenWaves.Web.Controls;
using OpenWaves.Web.ImageTransformations;

namespace OpenWaves.UnitTests.Web.Controls
{
    [TestFixture]
    public class ImageTests
    {
        [Test]
        public void Given_HideIfEmpty_is_true_When_ImageUrl_is_empty_Image_is_not_rendered()
        {
            Image image = new Image();
            image.HideIfEmpty = true;

            string output = RenderControl(image);

            Assert.AreEqual("", output);
        }

        [Test]
        public void When_ImageUrl_is_invalid_EmageTransformationFailed_event_is_raised()
        {
            var transformationServiceMock = new Mock<IWebImageTransformationService>();
            transformationServiceMock
                .Setup(s => s.GetTransformedImageUrl(It.IsAny<Url>(), It.IsAny<ImageTransformation>()))
                .Throws<ImageTransformationException>();

            using (ServiceLocator.SetScopedResolver(new BasicResolver().Register<IWebImageTransformationService>(transformationServiceMock.Object)))
            {
                Image image = new Image
                {
                    Transformation = ImageTransformationType.ScaleToFill,
                    Width = 100,
                    Height = 100,
                    ImageUrl = "/foo.jpg",
                };

                bool eventRaised = false;
                image.ImageTransformationFailed += (s, e) =>
                    {
                        eventRaised = true; 
                    };

                RenderControl(image);
                Assert.IsTrue(eventRaised);
            }
        }


        [Test]
        public void When_ImageUrl_is_invalid_Image_renders_empty_img_tag()
        {
            var transformationServiceMock = new Mock<IWebImageTransformationService>();
            transformationServiceMock
                .Setup(s => s.GetTransformedImageUrl(It.IsAny<Url>(), It.IsAny<ImageTransformation>()))
                .Throws<ImageTransformationException>();

            using (ServiceLocator.SetScopedResolver(new BasicResolver().Register<IWebImageTransformationService>(transformationServiceMock.Object)))
            {
                Image image = new Image
                {
                    Transformation = ImageTransformationType.ScaleToFit,
                    Width = 100,
                    Height = 100,
                    ImageUrl = "/foo.jpg",
                };

                var output = RenderControl(image);

                Assert.AreEqual("<img alt=\"\" />", output);
            }

        }

        [Test]
        public void When_ScaleToFill_transformation_is_used_Image_renders_width_and_height_style_attributes()
        {
            var transformationServiceMock = new Mock<IWebImageTransformationService>();
            transformationServiceMock
                .Setup(s => s.GetTransformedImageUrl(It.IsAny<Url>(), It.IsAny<ImageTransformation>()))
                .Returns(Url.Parse("obrazek.jpg"));
                
            using (ServiceLocator.SetScopedResolver(new BasicResolver().Register<IWebImageTransformationService>(transformationServiceMock.Object)))
            {
                Image image = new Image
                {
                    Transformation = ImageTransformationType.ScaleToFill,
                    Width = 100,
                    Height = 100,
                    ImageUrl = "/foo.jpg",
                };

                var output = RenderControl(image);

                Assert.That(output, Is.StringContaining("style=\"width:100px;height:100px;\""));
            }

        }

        [Test]
        public void When_None_transformation_type_is_used_and_width_and_heigth_are_not_set_then_apply_Null_transformation()
        {
            var image = new Image
            {
                Transformation = ImageTransformationType.None,
                ImageUrl = "/foo.jpg",
            };

            var output = RenderControl(image);

            Assert.That(output, Is.StringContaining("src=\"/foo.jpg\""));
        }

        private static string RenderControl(Image image)
        {
            var sb = new StringBuilder();
            using (var sw = new StringWriter(sb))
            {
                image.RenderControl(new HtmlTextWriter(sw));
            }

            return sb.ToString();
        }
    }
}